package com.tagtraum.perf.gcviewer;




import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class StyledTextArea extends JTextPane
{
  private static final Logger LOGGER = Logger.getLogger(StyledTextArea.class.getName());

  public static JScrollPane scrollable(String contents)
  {
    StyledTextArea textArea = new StyledTextArea(contents);
    JScrollPane pane = new JScrollPane(textArea);
    textArea.setCaretPosition(0);
    return pane;
  }

  public StyledTextArea(String contents)
  {
    HTMLEditorKit kit = new HTMLEditorKit();
    StyleSheet styleSheet = new StyleSheet();
    InputStreamReader is = new InputStreamReader(StyledTextArea.class.getResourceAsStream("style.css"));
    try {
      styleSheet.loadRules(is, null);
    } catch (IOException e) {
      LOGGER.log(Level.SEVERE, "Failed to load css", e);
    } finally {
      //Closers.closeResource(is, LOGGER);
    }

    kit.setStyleSheet(styleSheet);
    Document doc = kit.createDefaultDocument();

    setDocument(doc);
    setContentType("text/html");
    setEditable(false);
    setText(contents);
  }
}
